directives.directive('textObj', ["$rootScope", "tools", "$timeout", "$routeParams", "tmp", "filterServ", function ($rootScope, tools, $timeout, $routeParams, tmp, filterServ) {
    return {
        restrict: 'E',
        scope: true,
        replace: true,
        link: function (scope, element, attr) {
            var config = scope.config = $rootScope.getText(Number(attr.tmpid));
            var $container = $(element).parent();
            var base = $container.width();
            var $ele = $(element);
            var events = {};

            // 初始化
            // $ele.css({
            //     top: base * scope.config.top + 'px',
            //     left: base * scope.config.left + 'px',
            //     width: base * scope.config.width + 'px',
            //     height: base * scope.config.height + 'px'
            // });

            if (!config.content) {
                $ele.addClass(attr.type);
            }

            if (!config.mobileMode) {
                $ele.addClass('pa');
            } else {
                $ele.addClass('mb10');
                $ele.addClass('pr');
            }

            $ele.addClass('border').css({
                width: config.width <= 1 ? base * config.width + 'px' : config.width + 'px',
                height: config.height <= 1 ? base * config.height + 'px' : config.height + 'px'
            });

            // 初始化
            if (!tools.mobileEnv()) {
                $ele.css({
                    top: config.top <= 1 ? base * config.top + 'px' : config.top + 'px',
                    left: config.left <= 1 ? base * config.left + 'px' : config.left + 'px'
                });
            }

            if (config.content) {
                $ele.find('.text-container > div').css(config.style);
                scope.editable = false;
            } else {
                scope.editable = true;
            }

            scope.begin_edit = function (e) {
                $timeout(function () {
                    scope.editable = !scope.editable;
                    if (scope.editable) {
                        $ele.find('textarea').width($ele.width() - 20).height($ele.height() - 20);
                    }
                }, 0);
                e.stopPropagation();
            }

            scope.stopProp = function (e) {
                e.stopPropagation();
            }

            scope.recover_fields = function (flag) {
                $rootScope.$broadcast('FIELD_CLEAR_FROM_OBJECTS');
            }

            $ele.find('textarea').width(base * scope.config.width - 20).height(base * scope.config.height - 20);

            $ele.on('click', function (e) {
                e.stopPropagation();
            });

            if (base * config.top + $ele.height() > $container.height() - 100) {
                $container.height($container.height() + 150);
            }

            events.FONT_STYLE_CHANGE_EV_OFF = $rootScope.$on('FONT_STYLE_CHANGE_EV', function (e, key, value) {
                if (scope.selected) {
                    // angular.extend(config.style, styObj);
                    if (config.style[key]) {
                        config.style[key] = "";
                    } else {
                        config.style[key] = value;
                    }
                    $ele.find('.text-container > div').css(config.style);
                }
            });

            // 右键菜单
            var menus = [
                {
                    name: '样式',
                    icon: 'font icon',
                    submenus: [
                        {
                            name: '字体',
                            submenus: [
                                {
                                    name: '宋体',
                                    fn: function () {
                                        if (!config.style.fontFamily) {
                                            config.style.fontFamily = 'STFangsong,SimSun';
                                        } else {
                                            config.style.fontFamily = '';
                                        }
                                        $ele.find('.text-container > div').css(config.style);
                                    }
                                },
                                {
                                    name: '微软雅黑',
                                    fn: function () {
                                        if (!config.style.fontFamily) {
                                            config.style.fontFamily = 'Microsoft YaHei';
                                        } else {
                                            config.style.fontFamily = '';
                                        }
                                        $ele.find('.text-container > div').css(config.style);
                                    }
                                },
                                {
                                    name: '楷体',
                                    fn: function () {
                                        if (!config.style.fontFamily) {
                                            config.style.fontFamily = 'KaiTi,STKaiti';
                                        } else {
                                            config.style.fontFamily = '';
                                        }
                                        $ele.find('.text-container > div').css(config.style);
                                    }
                                }
                            ]
                        },
                        {
                            name: '下划线',
                            fn: function () {
                                if (!config.style.textDecoration) {
                                    config.style.textDecoration = 'underline';
                                } else {
                                    config.style.textDecoration = '';
                                }
                                $ele.find('.text-container > div').css(config.style);
                            }
                        },
                        {
                            name: '行间距',
                            submenus: [
                                {
                                    name: '1',
                                    fn: function () {
                                        config.style.lineHeight = 1;
                                        $ele.find('.text-container > div').css(config.style);
                                    }
                                },
                                {
                                    name: '1.5',
                                    fn: function () {
                                        config.style.lineHeight = 1.5;
                                        $ele.find('.text-container > div').css(config.style);
                                    }
                                },
                                {
                                    name: '2',
                                    fn: function () {
                                        config.style.lineHeight = 2;
                                        $ele.find('.text-container > div').css(config.style);
                                    }
                                }
                            ]
                        },
                        {
                            name: '删除线',
                            fn: function () {
                                if (!config.style.textDecoration) {
                                    config.style.textDecoration = 'line-through';
                                } else {
                                    config.style.textDecoration = '';
                                }
                                $ele.find('.text-container > div').css(config.style);
                            }
                        },
                        {
                            name: '加粗',
                            fn: function () {
                                if (!config.style.fontWeight) {
                                    config.style.fontWeight = 'bold';
                                } else {
                                    config.style.fontWeight = '';
                                }
                                $ele.find('.text-container > div').css(config.style);
                            }
                        },
                        {
                            name: '字号',
                            submenus: [
                                {
                                    name: '14px',
                                    fn: function () {
                                        config.style.fontSize = '14px';
                                        $ele.find('.text-container > div').css(config.style);
                                    }
                                },
                                {
                                    name: '16px',
                                    fn: function () {
                                        config.style.fontSize = '16px';
                                        $ele.find('.text-container > div').css(config.style);
                                    }
                                },
                                {
                                    name: '加大一号',
                                    fn: function () {
                                        config.style.fontSize = parseInt(config.style.fontSize) + 1 + 'px';
                                        $ele.find('.text-container > div').css(config.style);
                                    }
                                },
                                {
                                    name: '减小一号',
                                    fn: function () {
                                        config.style.fontSize = parseInt(config.style.fontSize) - 1 + 'px';
                                        $ele.find('.text-container > div').css(config.style);
                                    }
                                }
                            ]
                        }
                    ]
                },
                {
                    name: '清空',
                    icon: 'eraser icon',
                    fn: function () {
                        if (scope.config.content) {
                            events.CONFIRM_EV_OFF = $rootScope.$broadcast('CONFIRM_EV', {
                                title: '文本框清空提醒',
                                content: '文本清空之后不可恢复,确定清空吗?',
                                btns: {
                                    yes: {
                                        text: "确定",
                                        fn: function () {
                                            config.content = "";
                                        }
                                    },
                                    no: {
                                        text: '取消',
                                        fn: function () {

                                        }
                                    }
                                }
                            });
                        } else {
                            config.content = "";
                            $rootScope.$broadcast('CONFIRM_OFF_EV');
                            $rootScope.$broadcast('CONTEXTMENU_OFF_EV');
                        }
                    }
                },
                {
                    name: '删除',
                    icon: 'trash icon',
                    fn: function () {
                        if (scope.config.content) {
                            events.CONFIRM_EV_OFF = $rootScope.$broadcast('CONFIRM_EV', {
                                title: '文本框删除提醒',
                                content: '文本框删除之后不可恢复,确定删除吗?',
                                btns: {
                                    yes: {
                                        text: "确定删除",
                                        fn: function () {
                                            $rootScope.delText(config.tmpId);
                                            $rootScope.$broadcast('CONFIRM_OFF_EV');
                                            $rootScope.$broadcast('CONTEXTMENU_OFF_EV');
                                        }
                                    },
                                    no: {
                                        text: '取消',
                                        fn: function () {

                                        }
                                    }
                                }
                            });
                        } else {
                            $rootScope.delText(config.tmpId);
                            $rootScope.$broadcast('CONFIRM_OFF_EV');
                            $rootScope.$broadcast('CONTEXTMENU_OFF_EV');
                        }
                    }
                }
            ];

            // 右键操作
            element[0].addEventListener('contextmenu', function (e) {
                if (e.button == 2) {
                    $rootScope.$broadcast('CONTEXTMENU_EV', e.pageX, e.pageY, menus);
                    e.preventDefault();
                }
                e.stopPropagation();
                return false;
            });

            // 保存
            events.SAVE_EV_OFF = $rootScope.$on('SAVE_EV', function () {
                var base = $container.width();
                config.width = $ele.width() / base;
                config.height = $ele.height() / base;
                config.top = $ele.position().top / base;
                config.left = $ele.position().left / base;
                config.selected = false;
            });

            // 监听删除所有的事件
            events.DEL_ALL_THINGS_OFF = $rootScope.$on('DEL_ALL_THINGS', function () {
                $rootScope.delText(config.tmpId);
            });

            events.RESIZE_EV_OFF = $rootScope.$on('RESIZE_EV', function () {
                if (scope.selected) {
                    $ele.find('textarea').width(base * scope.config.width - 20).height(base * scope.config.height - 20);
                }
            });

            events.UNSELECT_EV_OFF = $rootScope.$on('UNSELECT_EV', function (e, identifier_) {
                if (scope.editable) {
                    scope.editable = false;
                }
            });

            events.UNSELECT_EV_OFF = $rootScope.$on('UNSELECT_EV', function (e, identifier_) {
                $timeout(function () {
                    scope.editable = false;
                }, 0);
            });

            scope.$on('$destroy', function () {
                for (var i in events) {
                    if (i.indexOf('_OFF') != -1) {
                        events[i]();
                    }
                }
            });
        },
        templateUrl: "/trend-new-BI/app/components/common/chartManager/text/textView.html"
    }
}]);